package com.golang.api.function;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.golang.api.common.ActivityStatusType;
import com.golang.api.common.SerialCodePrefix;
import com.golang.api.domain.*;
import com.golang.api.dto.request.*;
import com.golang.api.dto.response.*;


import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public interface ActivityFunction {





    default Function<List<Activity>, List<ActivityListRespDto>> pageInfos2Items(List<Activity> entities) {
        return dtos -> CollectionUtil.emptyIfNull(entities).stream().map(o -> new ActivityListRespDto()
                .setActivityId(o.getActivityId())
                .setActivityTitle(o.getActivityTitle())
                .setActivityType(o.getActivityType())
                .setStartTime(o.getStartTime())
                .setEndTime(o.getEndTime())
                .setActivityStatus(o.getActivityStatus())
                .setActivityStatusDesc(ActivityStatusType.getByDbType(o.getActivityStatus()).getShowName())
                .setActivityAddress(o.getActivityAddress())

        ).collect(Collectors.toList());
    }





    //创建活动基本信息
    default Function<ActivityBasicDto, Activity> activityBasic(ActivityBasicDto dto) {
        return a -> {
            Activity activity = new Activity()
                    .setActivityId(StrUtil.isEmpty(dto.getActivityId()) ? SerialCodePrefix.splice(SerialCodePrefix.ACTIVITY_ID) : dto.getActivityId())
                    .setActivityTitle(dto.getActivityTitle())  //活动标题
                    .setActivityIntroduction(dto.getActivityIntroduction()) //活动详情
                    .setActivityType(dto.getActivityType())
                    .setActivityClassification(dto.getActivityClassification().longValue())
                    .setLongitude(dto.getLongitude())
                    .setLatitude(dto.getLatitude())
                    .setActivityAddress(dto.getActivityAddress());

            //LocalDateTime parseStart = LocalDateTime.parse(dto.getStartTime(), DatePattern.NORM_DATE_FORMATTER);
            //LocalDateTime parseEnd = LocalDateTime.parse(dto.getEndTime(), DatePattern.NORM_DATETIME_FORMATTER);
            LocalDateTime parseStart = LocalDateTime.ofInstant(Instant.ofEpochMilli(dto.getStartTime()*1000L), ZoneId.systemDefault());
            LocalDateTime parseEnd = LocalDateTime.ofInstant(Instant.ofEpochMilli(dto.getEndTime()*1000L), ZoneId.systemDefault());


            activity.setStartTime(parseStart)
                    .setEndTime(parseEnd)
                    .setActivityStatus(ActivityStatusType.NotOpen.getDbType())
                    .setUpdateTime(LocalDateTime.now());

            if(StrUtil.isEmpty(dto.getActivityId())){
                activity.setCreateTime(LocalDateTime.now());
            }



            return activity;
        };
    }


    default Function<Activity, ActivityBasicRespDto> ActivityBasic2Dto(Activity entity) {

        return dto -> new ActivityBasicRespDto()
                .setActivityId(entity.getActivityId())
                .setActivityTitle(entity.getActivityTitle())
                .setActivityIntroduction(entity.getActivityIntroduction())
                .setActivityClassification(entity.getActivityClassification().intValue())
                .setActivityType(entity.getActivityType())
                .setActivityAddress(entity.getActivityAddress())
                .setStartTime(entity.getStartTime().format(DatePattern.NORM_DATETIME_FORMATTER))
                .setEndTime(entity.getEndTime().format(DatePattern.NORM_DATETIME_FORMATTER));


    }


    //创建活动报名设置
    default Function<ActivitySettingDto, Activity> activitySetting(ActivitySettingDto dto, Activity activity) {
        return a -> {
            activity.setCommunityId(dto.getCommunityId())
                    .setQuestion(dto.getQuestion()) //问题
                    .setPrivacyType(dto.getPrivacyType()) //隐私性
                    .setManagerPhone(dto.getManagerPhone())
                    .setManagerWechat(dto.getManagerWechat())
                    .setManagerWechatGroupPic(dto.getManagerWechatGroupPic());
            return activity;
        };
    }


    default Function<Activity, ActivitySettingRespDto> ActivitySetting2Dto(Activity entity) {

        return dto -> new ActivitySettingRespDto()
                .setActivityId(entity.getActivityId())
                .setCommunityId(entity.getCommunityId())
                .setQuestion(entity.getQuestion())
                .setManagerPhone(entity.getManagerPhone())
                .setManagerWechat(entity.getManagerWechat())
                .setManagerWechatGroupPic(entity.getManagerWechatGroupPic())
                .setPrivacyType(entity.getPrivacyType());
    }


    default Function<Activity, ActivityDetailDto> ActivityDetail2Dto(Activity entity) {

        return dto -> new ActivityDetailDto()
                .setActivityTitle(entity.getActivityTitle())
                .setActivityAddress(entity.getActivityAddress())
                .setStartTime(entity.getStartTime())
                .setEndTime(entity.getEndTime())
                .setActivityStatus(entity.getActivityStatus())
                .setActivityStatusDesc(ActivityStatusType.getByDbType(entity.getActivityStatus()).getShowName())
                .setActivityIntroduction(entity.getActivityIntroduction())
                .setLowPrice(null);


    }


    //创建活动流程信息
    default Function<ActivityProcessDto, ActivityProcess> activityProcessDto2ActivityProcess(ActivityProcessDto dto, String activityId) {
        return entity -> {
            ActivityProcess activityProcess = new ActivityProcess();
            activityProcess.setActivityId(activityId);
            activityProcess.setProcessTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(dto.getProcessTime()*1000L), ZoneId.systemDefault()));

            return activityProcess;
        };
    }


    default Function<ActivityProcess, ActivityProcessRespDto> ActivityProcess2Dto(ActivityProcess entity) {
        return dto -> new ActivityProcessRespDto()
                .setId(entity.getId())
                .setProcessTime(entity.getProcessTime().format(DatePattern.NORM_DATETIME_FORMATTER))
                .setProcessContent(entity.getProcessContent());
    }


    default Function<ActivityTag, ActivityTagRespDto> ActivityTag2Dto(ActivityTag entity) {
        return dto -> new ActivityTagRespDto()
                .setTagId(entity.getTagId())
                .setTagName(entity.getTagName());
    }

    default Function<ActivityTicket, ActivityTicketRespDto> ActivityTicket2Dto(ActivityTicket entity) {
        return dto -> new ActivityTicketRespDto()
                .setTicketName(entity.getTicketName())
                .setPrice(entity.getPrice())
                .setEnrollMaxCount(entity.getEnrollMaxCount())
                .setLimitQuantity(entity.getLimitQuantity())
                .setBuyDeadline(entity.getBuyDeadline().format(DatePattern.NORM_DATE_FORMATTER));
    }


    default Function<ActivityTicket, ActivityTicketInfoDto> ActivityTicketInfo2Dto(ActivityTicket entity) {
        return dto -> new ActivityTicketInfoDto()
                .setTicketName(entity.getTicketName())
                .setTicketType(entity.getTicketType())
                .setEnrollMaxCount(entity.getEnrollMaxCount())
                .setLimitQuantity(entity.getLimitQuantity());

    }


    default Function<ActivityTicketDto, ActivityTicket> activityTicketDto2ActivityTicket(ActivityTicketDto dto, String activityId, long refundDeadline) {
        return entity -> new ActivityTicket()
                .setActivityId(activityId)
                .setTicketName(dto.getTicketName())
                .setPrice(dto.getPrice())
                .setEnrollMaxCount(dto.getEnrollMaxCount())
                .setLimitQuantity(dto.getLimitQuantity())
                .setBuyDeadline(LocalDateTime.ofInstant(Instant.ofEpochMilli(dto.getBuyDeadline()*1000L), ZoneId.systemDefault()))
                .setRefundDeadline(LocalDateTime.ofInstant(Instant.ofEpochMilli(refundDeadline*1000L), ZoneId.systemDefault()));
    }


    default Function<ActivityTagDto, ActivityTag> activityTagDto2ActivityTag(ActivityTagDto dto, String activityId) {
        return entity -> new ActivityTag()
                .setActivityId(activityId)
                .setTagId(dto.getTagId())
                .setTagName(dto.getTagName());
    }



    default Function<String, ActivityPic> activityPicDto2ActivityPic(String pic, String activityId) {
        return entity -> new ActivityPic()
                .setActivityId(activityId)
                .setPic(pic);
    }

}
